<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/15
 * Time: 11:29
 */

namespace App\Repositories;

use App\Models\OrderList;

use App\Models\PutPeriod;
use App\Models\Subscribe;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;

class OrderListRepository extends BaseRepository
{

    protected $studenttime;

    public function __construct(
        OrderList $order_list
    )
    {
        $this->model = $order_list;
    }

    /**
     * 资源列表
     *
     * @param  array $data 必须传入与模型查询相关的数据
     * @param  string|array $extra 可选额外传入的参数
     * @param  string $size 分页大小（存在默认值）
     * @return Illuminate\Support\Collection
     */
    public function index($data='', $extra='', $size='10')
    {
        $list = $this->model->where('userid', $data->id)->where('ordstatus', 1)->orderBy('id', 'desc')->get();
        $total = $this->model->where('userid', $data->id)->where('ordstatus', 1)->sum('ordfee');

        if($list){

            foreach($list as $data){
                $sub_info = Subscribe::find($data->productid);
                $peroid_info = '';
                if($sub_info){

                    $peroid_info = PutPeriod::where('yy_put_periods.id', $sub_info->putime_id)->leftJoin('coachs','coachs.id','=','yy_put_periods.coach_id')
                        ->select('yy_put_periods.*','coachs.name as coachname')->first();
                }

                $data->coachname = '';
                $data->km = '';
                $data->startime = '';
                $data->endtime = '';
                $data->order_pay_type = 0;
                if($peroid_info){
                    $data->coachname = $peroid_info->coach->name;
                    $data->km = $peroid_info->km;
                    $data->startime = $peroid_info->startime;
                    $data->endtime = $peroid_info->endtime;
                    $data->order_pay_type = 1;
                }
            }
        }

        return ['paymoney' => number_format($total,2), 'list' => $list];
    }

    /**
     * 编辑特定id资源
     *
     * @param  int $id 资源id
     * @param  string|array $extra 可选额外传入的参数
     * @return Illuminate\Support\Collection
     */
    public function edit($id, $extra)
    {
        return true;
    }


    public function store($product_info, $buyer)
    {
        $this->model->userid = $buyer->item->id;
        $this->model->username = $buyer->item->name;
        $this->model->ordid = date('YmdHis').rand(1000000,9999999);
        $this->model->ordtime = time();
        $this->model->ordtitle = $product_info['title'];//` varchar(255) DEFAULT NULL COMMENT '订单标题',
        $this->model->ordbuynum = $product_info['num'];
        $this->model->ordprice = $product_info['price'];//` float(10,2) DEFAULT '0.00' COMMENT '产品单价',
        $this->model->ordfee = $product_info['price'] *$product_info['num'];//` float(10,2) DEFAULT '0.00' COMMENT '订单总金额',,
        $this->model->payment_buyer_email = $buyer->email;//` varchar(255) DEFAULT NULL,
        $this->model->productid = isset($product_info['id']) ? $product_info['id'] : '';
        $this->model->school_id = $buyer->item->school_id;
        $this->model->type = isset($product_info['id']) ? 2 : 1;
        $this->model->km = 1;
        if($this->model->save()){
            return $this->model;
        }

        return false;

    }

}